package com.scau402.system1.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scau402.system1.domain.dto.WmsAuditOrderDTO;
import com.scau402.system1.domain.factory.WmsAuditOrderFactory;
import com.scau402.system1.domain.po.*;
import com.scau402.system1.domain.query.WmsAdjustBoundOrderQuery;
import com.scau402.system1.domain.vo.WmsInboundOrderAndDetailVO;
import com.scau402.system1.domain.vo.WmsOutboundOrderAndDetailVO;
import com.scau402.system1.mapper.IWmsAuditOrderMapper;
import com.scau402.system1.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 审核模块Repository实现类
 * @since 2024/2/5
 */
@Repository
public class WmsAuditOrderRepositoryImpl implements IWmsAuditOrderRepository {
    @Autowired
    private IWmsAuditOrderMapper auditOrderMapper;
    @Autowired
    private WmsAuditOrderFactory auditOrderFactory;
    @Autowired
    private IWmsAdjustDetailRepository adjustDetailRepo;
    @Autowired
    private IWmsOutboundOrderRepository outboundOrderRepo;
    @Autowired
    private IWmsInboundOrderRepository inboundOrderRepo;
    @Autowired
    private IWmsCheckJadeDetailRepository checkJadeDetailRepo;

    /**
     * 根据id列表搜索未审核的审核单
     * @param ids id列表
     * @since 2024/2/5
     */
    @Override
    public List<WmsAuditOrderPO> findUnAuditList(List<Integer> ids) {
        // 搜索ids中状态为"待审核"的审核单数据
        return Optional.ofNullable(auditOrderMapper.selectList(new QueryWrapper<WmsAuditOrderPO>().in("id", ids).eq("status", '0')))
                .orElse(new ArrayList<>());
    }

    /**
     * 更新审核单
     * @param auditList 审核单列表
     * @since 2024/2/5
     */
    @Override
    public void update(List<WmsAuditOrderPO> auditList) {
        for (WmsAuditOrderPO auditOrder : auditList) {
            auditOrderMapper.update(auditOrder,new QueryWrapper<WmsAuditOrderPO>().eq("id",auditOrder.getId()));
        }
    }

    /**
     * 插入审核单
     * @param po 审核单信息
     * @since 2024/2/5
     */
    @Override
    public void save(WmsAuditOrderPO po) {
        auditOrderMapper.insert(po);
    }

    /**
     * 获取库存调整审核单信息列表
     * @param adjustBoundOrderQuery 查询条件
     * @return 审核单信息DTO列表
     * @since 2024/2/6
     */
    @Override
    public List<WmsAuditOrderDTO<WmsAdjustBoundOrderDetailPO>> findAdjustList(WmsAdjustBoundOrderQuery adjustBoundOrderQuery) {
        //搜索相关审核单
        List<WmsAuditOrderPO> poList = selectListByTenantIdAndType(adjustBoundOrderQuery.getTenantId(),adjustBoundOrderQuery,"2");
        //搜索审核单相关数据
        List<WmsAuditOrderDTO<WmsAdjustBoundOrderDetailPO>> dtoList =
                poList.stream().map(po -> auditOrderFactory.POToDTO(po,adjustDetailRepo.findAdjustByAudit(adjustBoundOrderQuery.setAudit(po.getId()))))
                        .collect(Collectors.toList());

        return dtoList;
    }


    /**
     * 获取出借审核单信息列表
     * @param tenantId 租户id
     * @return 审核单信息DTO列表
     * @since 2024/2/12
     */
    @Override
    public List<WmsAuditOrderDTO<WmsOutboundOrderAndDetailVO>> findOutBoundList(Long tenantId) {
        List<WmsAuditOrderPO> poList = selectListByTenantIdAndType(tenantId,new WmsAdjustBoundOrderQuery(), "1");

        //搜索审核单相关数据
        List<WmsAuditOrderDTO<WmsOutboundOrderAndDetailVO>> dtoList =
                poList.stream().map(po -> auditOrderFactory.POToDTO(po,outboundOrderRepo.findOutboundByAudit(po)))
                .collect(Collectors.toList());

        return dtoList;
    }

    /**
     * 获取入库审核单信息列表
     * @param tenantId 租户id
     * @return 审核单信息DTO列表
     * @since 2024/2/19
     */
    @Override
    public List<WmsAuditOrderDTO<WmsInboundOrderAndDetailVO>> findInBoundList(Long tenantId) {
        //查询相关审核单
        List<WmsAuditOrderPO> poList = selectListByTenantIdAndType(tenantId,new WmsAdjustBoundOrderQuery(), "3");

        //搜索审核单相关数据
        List<WmsAuditOrderDTO<WmsInboundOrderAndDetailVO>> dtoList =
                poList.stream().map(po -> auditOrderFactory.POToDTO(po, inboundOrderRepo.findInboundAndDetailByAudit(po)))
                .collect(Collectors.toList());
        return dtoList;
    }
/**
 * 获取价格调整审核单信息列表
 */
    @Override
    public List<WmsAuditOrderDTO<WmsAdjustPriceOrderDetailPO>> findPriceAdjustList(WmsAdjustBoundOrderQuery adjustBoundOrderQuery) {
        //查询相关审核单
        List<WmsAuditOrderPO> poList = selectListByTenantIdAndType(adjustBoundOrderQuery.getTenantId(),new WmsAdjustBoundOrderQuery(), "0");
        //TODO
//        //搜索审核单相关数据
//        List<WmsAuditOrderDTO<WmsAdjustPriceOrderDetailPO>> dtoList =
//                poList.stream().map(po -> auditOrderFactory.POToDTO(po,adjustDetailRepo.findAdjustByAudit(adjustBoundOrderQuery.setAudit(po.getId()))))
//                        .collect(Collectors.toList());
//        return dtoList;
        return  null;
    }

    /**
     * 根据查询条件adjustBoundOrderQuery和审核type获取审核单PO列表
     * @param adjustBoundOrderQuery 查询条件
     * @param type 审核type
     * @return 2024/2/12
     */
    public List<WmsAuditOrderPO> selectListByTenantIdAndType(Long tenantId,WmsAdjustBoundOrderQuery adjustBoundOrderQuery,String type){
        return auditOrderMapper.selectList(new QueryWrapper<WmsAuditOrderPO>()
                        .eq("tenant_id", tenantId)
                        .eq("type", type)
                        .ge(adjustBoundOrderQuery.getStartTime()!=null,"create_time",adjustBoundOrderQuery.getStartTime())
                        .le(adjustBoundOrderQuery.getEndTime()!=null,"create_time",adjustBoundOrderQuery.getEndTime())
                        .orderByDesc("create_time")
                );
    }

}
